package com.younio.local.util;

import java.util.Collection;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @desc:
 * @author: lan     [1728209643@qq.com]
 * @date: 2018-08-16 18:14
 */
public final class Objects {

    public static <T> void test(boolean express, T t, Consumer<T> consumer) {
        if (!express) {
            consumer.accept(t);
        }
    }

    public static <T> T getOrDefault(T t, T defaultT) {
        if (t == null) {
            return defaultT;
        }
        if (t instanceof String && strIsEmpty((String) t)) {
            return defaultT;
        }
        return t;
    }

    @SuppressWarnings("unchecked")
    public static int equals(Comparable o1, Comparable o2) {
        return o1 == o2 ? 0 : o1 != null ? o1.compareTo(o2) : -1;
    }

    public static Integer defaultValue(Integer integer) {
        if (integer == null) {
            integer = 0;
        }
        return integer;
    }

    public static Long defaultValue(Long lon) {
        if (lon == null) {
            lon = 0L;
        }
        return lon;
    }

    public static <E> Collection<E> requireNonNullOrEmpty(Collection<E> collection, String message) {
        requireNonNull(collection, message);
        if (collection.isEmpty()) {
            throw new RuntimeException(message);
        }
        return collection;
    }

    public static <E extends Collection> E emptyToNull(E collection) {
        if (collection == null || collection.isEmpty()) {
            return null;
        }
        return collection;
    }

    public static <E extends Map> E emptyToNull(E map) {
        if (map == null || map.isEmpty()) {
            return null;
        }
        return map;
    }

    public static <E extends Map> E requireNonNullOrEmpty(E map, String message) {
        requireNonNull(map, message);
        if (map.isEmpty()) {
            throw new RuntimeException(message);
        }
        return map;
    }

    public static String getNotNullStr(String str) {
        if (strIsEmpty(str)) {
            return "";
        }
        return str;
    }

    public static void checkArguments(boolean express, String message) {
        if (!express) {
            throw new RuntimeException(message);
        }
    }

    public static boolean isNull(Object o) {
        return o == null;
    }

    public static void requireNull(Object obj, String message) {
        if (obj != null)
            throw new RuntimeException(message);
    }

    public static <T> T requireNonNull(T t, String message) {
        try {
            if (t != null) {
                if (t instanceof String) {
                    if (strIsEmpty((String) t)) {
                        throw new RuntimeException(message);
                    }
                }
            }
            return java.util.Objects.requireNonNull(t, message);
        } catch (Exception e) {
            throw new RuntimeException(message);
        }
    }

    public static boolean strIsEmpty(String string) {
        return string == null || string.length() == 0;
    }

    public static boolean strIsNotEmpty(String string) {
        return !strIsEmpty(string);
    }

    public static boolean arrIsEmpty(Object[] objects) {
        return objects == null || objects.length == 0;
    }
}
